package my.edu.model.bills.sales.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.NotNull;
import my.edu.common.domain.IdsRequest;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.PageVO;
import my.edu.common.domain.Response;
import my.edu.common.validate.groups.Insert;
import my.edu.model.bills.sales.entity.dto.SalesOrderSearchDTO;
import my.edu.model.bills.sales.entity.dto.SalesReturnDTO;
import my.edu.model.bills.sales.entity.dto.SalesReturnOrderSearchDTO;
import my.edu.model.bills.sales.entity.vo.SalesReturnVO;
import my.edu.model.bills.sales.service.ISalesReturnOrderService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salesReturnOrder")
public class SalesReturnOrderController {
    @Autowired
    private ISalesReturnOrderService salesReturnOrderService;

    @PutMapping
    @RequiresPermissions("salesReturn:save")
    @Operation(summary = "新增订单")
    public Response addSalesReturnOrder(@RequestBody @Validated(Insert.class) SalesReturnDTO salesReturnDTO) {
        salesReturnOrderService.saveNew(salesReturnDTO);
        return Response.success();
    }

    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("salesReturn:del")
    @Operation(summary = "批量删除订单")
    public Response delSalesOrder(String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        salesReturnOrderService.removeOrder(longs);
        return Response.success();
    }

    @DeleteMapping("/delete")
    @RequiresPermissions("salesReturn:del")
    @Operation(summary = "删除订单")
    public Response delete(Long id) {
        salesReturnOrderService.removeOrder(List.of(id));
        return Response.success();
    }

    @PostMapping("/update")
    @RequiresPermissions("salesReturn:update")
    @Operation(summary = "修改订单")
    public Response updateSalesReturnOrder(@RequestBody @Validated SalesReturnDTO salesReturnDTO) {
        salesReturnOrderService.updateOrder(salesReturnDTO);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"salesReturn:audit"})
    @Operation(summary = "更新审核状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        List<Long> longs = StringUtils.strToLongList(ids);
        if(status==1){
            salesReturnOrderService.auditOrder(longs);
        } else if (status==0) {
            salesReturnOrderService.rauditOrder(longs);
        } else{
            return Response.error("状态值错误");
        }
        return Response.success();
    }

    @GetMapping("/list")
    @RequiresPermissions("salesReturn:get")
    @Operation(summary = "查询订单")
    public Response getSalesReturnrder(@ModelAttribute @Validated PageDTO page,
                                     @ModelAttribute SalesReturnOrderSearchDTO salesReturnOrderSearchDTO) {
        return Response.success( salesReturnOrderService.getSalesOrder(page, salesReturnOrderSearchDTO));
    }

    @GetMapping("/one")
    @RequiresPermissions("salesReturn:get")
    @Operation(summary = "根据id查询查询订单")
    public Response getSalesReturnOrder(@PathVariable @NotNull(message = "id不能为空") Long id) {
        SalesReturnVO salesReturnVO = salesReturnOrderService.getSalesOrderOne(id);
        return Response.success(salesReturnVO);
    }

    @GetMapping
    @Operation(summary = "查询全部订单")
    public Response getOnly(@ModelAttribute SalesReturnOrderSearchDTO salesReturnOrderSearchDTO) {
        return Response.success( salesReturnOrderService.listOnly(salesReturnOrderSearchDTO));
    }


}
